package com.bluemson.controller;

import com.bluemson.entity.Article;
import com.bluemson.entity.Comment;
import com.bluemson.entity.Concern;
import com.bluemson.entity.User;
import com.bluemson.service.AddService;
import com.bluemson.service.CheckService;
import com.bluemson.service.Impl.AddServiceImpl;
import com.bluemson.service.Impl.CheckServiceImpl;
import com.bluemson.service.Impl.UpdateServiceImpl;
import com.bluemson.service.UpdateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = {"/register"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map register(@RequestBody User user) {
        AddService addService = new AddServiceImpl();
        Map map = new HashMap();
        if (addService.addUser(user)) {
            map.put("msg", "注册成功");
            map.put("status", 1);
        } else {
            map.put("msg", "注册失败,用户名重复");
            map.put("status", 0);
        }
        return map;
    }

    @RequestMapping(value = {"/login"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map login(@RequestBody User user,HttpServletRequest request) {
        CheckService checkService = new CheckServiceImpl();
        Map map = new HashMap();
        User user1 = checkService.checkLogin(user);
        HttpSession session = request.getSession();
        if (user1!=null) {
            session.setAttribute("currentUser",user);
            map.put("msg", "登录成功");
            map.put("user", user1);
            map.put("status", 1);
        } else {
            map.put("msg", "登录失败");
            map.put("status", 0);
        }
        return map;
    }
    @RequestMapping(value = "/exit",method = RequestMethod.POST)
    @ResponseBody
    public Map exitLogin(@RequestBody User user,HttpServletRequest request){
        Map map = new HashMap();
        CheckService checkService = new CheckServiceImpl();
        checkService.exitLogin(user);
        HttpSession session = request.getSession();
        session.setAttribute("currentUser",null);
        map.put("msg","退出登录成功");
        map.put("status",0);
        return map;
    }

    @PostMapping("/addHead")
    @ResponseBody
    public Map addHead(@RequestParam("head")MultipartFile head,@RequestParam("id")int id,HttpServletRequest request) throws IOException {
        AddService addService = new AddServiceImpl();
        String uri = addService.addHead(head,request,id);
        Map map = new HashMap();
        map.put("msg","设置头像成功");
        map.put("uri",uri);
        map.put("status",1);
        return map;
    }
    @PostMapping("/addFile")
    @ResponseBody
    public Map addFile(@RequestParam("file")MultipartFile file,@RequestParam("id")int id,HttpServletRequest request) throws IOException {
        AddService addService = new AddServiceImpl();
        String uri = addService.addFile(file,request,id);
        Map map = new HashMap();
        map.put("msg","添加附件成功");
        map.put("uri",uri);
        map.put("status",1);
        return map;
    }

    @RequestMapping(value = {"/addArticle"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map addArticle(@RequestBody Article article) {
        Map map = new HashMap();
        AddService addService = new AddServiceImpl();
//        int id = addService.addArticle(article);
        addService.addArticle(article);
        map.put("msg", "发帖成功");
//        map.put("articleId",id);
        map.put("status", 1);
        return map;
    }
    @RequestMapping(value = "/like",method = RequestMethod.POST)
    @ResponseBody
    public Map like(@RequestBody Article article){
        Map map = new HashMap();
        UpdateService updateService = new UpdateServiceImpl();
        updateService.like(article);
        map.put("msg","点赞成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = "/checkLike",method = RequestMethod.POST)
    @ResponseBody
    public Map checkLike(@RequestBody Article article){
        Map map = new HashMap();
        CheckService checkService = new CheckServiceImpl();
        if(checkService.checkLike(article)){
            map.put("msg","未点赞");
            map.put("status",1);
        }
        else {
            map.put("msg","已点赞");
            map.put("status",0);
        }
        return map;
    }
    @RequestMapping(value = {"/concernPlate"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map concernPlate(@RequestBody Concern concern){
        Map map = new HashMap();
        AddService addService = new AddServiceImpl();
        if(addService.concernPlate(concern)){
            map.put("msg","关注该板块成功");
            map.put("status",1);
        }
        else {
            map.put("msg","该用户已经关注该板块");
            map.put("staus",0);
        }
        return map;
    }
    @RequestMapping(value = {"/checkConcern"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map checkConcern(@RequestBody Concern concern){
        Map map = new HashMap();
        CheckService checkService = new CheckServiceImpl();
        if(checkService.checkConcern(concern)){
            map.put("msg","该用户未关注该板块");
            map.put("status",0);
        }else {
            map.put("msg","该用户已经关注该板块");
            map.put("status",1);
        }
        return map;
    }
    @RequestMapping(value = {"/addComment"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map addComment(@RequestBody Comment comment){
        Map map = new HashMap();
        AddService addService = new AddServiceImpl();
        addService.addComment(comment);
        map.put("msg","评论成功");
        map.put("status",1);
        return map;
    }
    @RequestMapping(value = {"/addReply"}, method = {RequestMethod.POST})
    @ResponseBody
    public Map addReply(@RequestBody Comment comment){
        Map map = new HashMap();
        AddService addService = new AddServiceImpl();
        addService.addReply(comment);
        map.put("msg","回复评论成功");
        map.put("status",1);
        return map;
    }
//    @RequestMapping(value = "/addArticle",method = RequestMethod.POST)
//    @ResponseBody
//    public ImgResultDto uploadImg(@RequestParam("myFileName") List<MultipartFile> myFileName,@RequestParam("username")String username,
//                                  @RequestParam("title")String title,@RequestParam("plate")String plate, HttpServletRequest request) throws IOException {
//        //这里是我在web中定义的两个初始化属性，保存目录的绝对路径和相对路径，你们可以自定义
//        AddService addService = new AddServiceImpl();
////        int articleId = Integer.parseInt(request.getServletContext().getInitParameter("articleId"));
//        //服务曾处理数据，返回Dto
//        Article article = new Article();
//        article.setUsername(username);
//        article.setPlate(plate);
//        return addService.upLoadImg(myFileName, addService.addArticle(article),request);
//    }
}
//        String realName = "";
//        String realPath = "";
//        if (myFileName != null) {
//            String fileName = myFileName.getOriginalFilename();
//            assert fileName != null;
//            String fileNameExtension = fileName.substring(fileName.indexOf("."),fileName.length());
//            realName = UUID.randomUUID().toString() + fileNameExtension;
//            realPath = request.getSession().getServletContext().getRealPath("/upload");
////			realPath = "E:\\work\\Java\\DMS\\WebContent\\upload";
//            File uploadFile = new File(realPath, realName);
//            myFileName.transferTo(uploadFile);
//        }
//        Map map = new HashMap();
//        map.put("url", request.getContextPath() + "/upload/" + realName);
//        map.put("img",new ImgResultDto("0", Arrays.asList(request.getContextPath() + "/upload/" + realName)));
//        System.out.println(map);
//        return map;
//    }
//}
